package yxy.game.pm2.command.resonate;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ResonateMessage;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.resonate.ResonateHandle;
import yxy.game.pm2.module.resonate.ResonateHeroMsg;
import yxy.game.pm2.module.resonate.ResonateModule;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author ljb
 * @name CMD_ResonateHeros
 * @descrition 共鸣系统共鸣武将列表
 * @since 2022-07-27
 */
@MessageType(proto = Command.ResonateHeros, classtype = ResonateMessage.ResonateHerosRequest.class)
public class CMD_ResonateHeros extends Command<ResonateMessage.ResonateHerosRequest> {

    @Override
    public void execute(MessageSource messageSource, ResonateMessage.ResonateHerosRequest request) {
        String playerCode = messageSource.getTag();

        ResonateMessage.ResonateHerosResponse.Builder response = ResonateMessage.ResonateHerosResponse.newBuilder();

        try {
            ResonateModule module = ResonateModule.get(playerCode);

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            //todo 获取已共鸣的武将信息
            ArrayList<String> codes = new ArrayList<>();
            ArrayList<ResonateHeroMsg> resonateHeros = module.getResonateHeros();
            for (ResonateHeroMsg hero : resonateHeros) {
                codes.add(hero.getCode());
            }

            //todo 获取阵上的武将
            ArrayList<String> aboveHeroCodeList = ResonateHandle.getAboveHeroCodeList(module);

            //todo 获取所有资质20以上的武将,需要排除已共鸣的武将
            HeroHandle heroHandle = new HeroHandle();
            Collection<Hero> heros = heroHandle.getHeros(playerCode);
            for (Hero hero : heros) {
                herodata data = DataCenter.getData(hero.getDataId(), herodata.class);
                if (data.getAptitude() < ResonateModule.resonateHeroQuality) continue;  //资质20以上
                if (codes.contains(hero.getCode())) continue;                           //已共鸣的武将不需要在其中显示
                if (aboveHeroCodeList.contains(hero.getCode())) continue;                 //阵上的武将不需要在其中显示
                ResonateMessage.HeroMsg.Builder builder = ResonateMessage.HeroMsg.newBuilder();
                builder.setCode(hero.getCode());
                builder.setHeroId(hero.getDataId());
                builder.setStar(hero.getStar());
                builder.setStage(hero.getStage());
                builder.setLevel(hero.getLevel());
                response.addHeroList(builder.build());
            }

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, ResonateMessage.ResonateHerosResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}